package com.eas.biz.service.statistic.impl;

import java.util.LinkedList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.eas.biz.dao.statistic.dataobject.StudentQuestionStatisticDO;
import com.eas.biz.dao.statistic.dataobject.StudentStatisticQuery;
import com.eas.biz.dao.statistic.dataobject.StudentTagStatisticDO;
import com.eas.biz.dao.statistic.interfaces.StudentQuestionStatisticDao;
import com.eas.biz.dao.statistic.interfaces.StudentTagStatisticDao;
import com.eas.biz.service.exam.interfaces.ExamService;
import com.eas.biz.service.statistic.domain.QuestionTypeStatistic;
import com.eas.biz.service.statistic.domain.StudentSubjectStatistic;
import com.eas.biz.service.statistic.domain.TagStatistic;
import com.eas.biz.service.statistic.interfaces.StudentStatisticService;

@Service("studentStatisticService")
public class StudentStatisticServiceImpl implements StudentStatisticService {

    @Autowired
    private StudentTagStatisticDao studentTagStatisticDao;

    @Autowired
    private StudentQuestionStatisticDao studentQuestionStatisticDao;

    @Resource(name = "examCacheService")
    private ExamService examService;

    public List<StudentSubjectStatistic> listTagStatistic(long studentId, String subject, String tag) {
        List<StudentSubjectStatistic> list = new LinkedList<StudentSubjectStatistic>();
        if (studentId <= 0 || StringUtils.isBlank(subject) || StringUtils.isBlank(tag)) {
            return list;
        }

        StudentStatisticQuery query = new StudentStatisticQuery();
        query.setTag(tag);
        query.setStudentId(studentId);
        query.setSubject(subject);
        List<StudentTagStatisticDO> result = studentTagStatisticDao.selectByQuery(query);
        if (list != null) {
            for (StudentTagStatisticDO tagDO : result) {
                StudentSubjectStatistic sss = buildStudentStatistic(tagDO);
                sss.setExamInfo(examService.getSimpleExam(sss.getExamId()));
                sss.addTagCount(buildTagCount(tagDO));
                list.add(sss);
            }
        }
        return list;
    }

    public List<StudentSubjectStatistic> listTypeStatistic(long studentId, String subject,
            String questionType) {
        List<StudentSubjectStatistic> list = new LinkedList<StudentSubjectStatistic>();
        if (studentId <= 0 || StringUtils.isBlank(subject) || StringUtils.isBlank(questionType)) {
            return list;
        }

        StudentStatisticQuery query = new StudentStatisticQuery();
        query.setQuestionType(questionType);
        query.setStudentId(studentId);
        query.setSubject(subject);
        List<StudentQuestionStatisticDO> result = studentQuestionStatisticDao.selectByQuery(query);
        if (list != null) {
            for (StudentQuestionStatisticDO tagDO : result) {
                StudentSubjectStatistic sss = buildStudentStatistic(tagDO);
                sss.setExamInfo(examService.getSimpleExam(sss.getExamId()));
                sss.addQuestionTypeCount(buildQuestionTypeCount(tagDO));
                list.add(sss);
            }
        }
        return list;
    }

    public StudentSubjectStatistic getTagStatistic(long examId, long studentId, String subject,
            boolean orderByErrorCount, int maxTagCount) {
        if (StringUtils.isBlank(subject)) {
            return null;
        }

        StudentStatisticQuery query = new StudentStatisticQuery();
        query.setExamId(examId);
        query.setStudentId(studentId);
        query.setSubject(subject);
        if (orderByErrorCount) {
            query.setOrderByErrorCountDesc("true");
        } else {
            query.setOrderByCorrectCountDesc("true");
        }
        if (maxTagCount > 0) {
            query.setPageNumber(1);
            query.setPageSize(maxTagCount);
        }
        List<StudentTagStatisticDO> list = studentTagStatisticDao.selectByQuery(query);

        StudentSubjectStatistic sss = null;
        if (list != null) {
            for (StudentTagStatisticDO tagDO : list) {
                if (sss == null) {
                    sss = buildStudentStatistic(tagDO);
                }

                sss.addTagCount(buildTagCount(tagDO));
            }
        }
        return sss;
    }

    public StudentSubjectStatistic getQuestionTypeStatistic(long examId, long studentId,
            String subject, boolean orderByErrorCount) {
        if (StringUtils.isBlank(subject)) {
            return null;
        }

        StudentStatisticQuery query = new StudentStatisticQuery();
        query.setExamId(examId);
        query.setStudentId(studentId);
        query.setSubject(subject);
        if (orderByErrorCount) {
            query.setOrderByErrorCountDesc("true");
        } else {
            query.setOrderByCorrectCountDesc("true");
        }
        List<StudentQuestionStatisticDO> list = studentQuestionStatisticDao.selectByQuery(query);

        StudentSubjectStatistic sss = null;
        if (list != null) {
            for (StudentQuestionStatisticDO questionDO : list) {
                if (sss == null) {
                    sss = buildStudentStatistic(questionDO);
                }
                sss.addQuestionTypeCount(buildQuestionTypeCount(questionDO));
            }
        }
        return sss;
    }

    private QuestionTypeStatistic buildQuestionTypeCount(StudentQuestionStatisticDO questionDO) {
        QuestionTypeStatistic count = new QuestionTypeStatistic();
        count.setQuestionType(questionDO.getQuestionType());
        count.setTotalCount(questionDO.getTotalCount());
        count.setErrorCount(questionDO.getErrorCount());
        count.setCorrectCount(questionDO.getCorrectCount());
        count.setTotalScore(questionDO.getTotalScore());
        count.setScore(questionDO.getScore());
        return count;
    }

    private TagStatistic buildTagCount(StudentTagStatisticDO tagDO) {
        TagStatistic tagCount = new TagStatistic();
        tagCount.setTag(tagDO.getTag());
        tagCount.setTotalCount(tagDO.getTotalCount());
        tagCount.setErrorCount(tagDO.getErrorCount());
        tagCount.setCorrectCount(tagDO.getCorrectCount());
        tagCount.setTotalScore(tagDO.getTotalScore());
        tagCount.setScore(tagDO.getScore());
        return tagCount;
    }

    private StudentSubjectStatistic buildStudentStatistic(StudentTagStatisticDO tagDO) {
        StudentSubjectStatistic sss = new StudentSubjectStatistic();
        sss.setExamId(tagDO.getExamId());
        sss.setStudentId(tagDO.getStudentId());
        sss.setClassId(tagDO.getClassId());
        sss.setSubject(tagDO.getSubject());
        return sss;
    }

    private StudentSubjectStatistic buildStudentStatistic(StudentQuestionStatisticDO questionDO) {
        StudentSubjectStatistic sss = new StudentSubjectStatistic();
        sss.setExamId(questionDO.getExamId());
        sss.setStudentId(questionDO.getStudentId());
        sss.setClassId(questionDO.getClassId());
        sss.setSubject(questionDO.getSubject());
        return sss;
    }
}
